import 'dart:async';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:huxiaodui/api/api.dart';
import 'package:huxiaodui/api/model/result.dart';
import 'package:huxiaodui/api/model/alarm_model.dart';
import 'package:huxiaodui/api/model/mobile_app_model.dart';
import 'package:huxiaodui/background_run/bg_channel.dart';
import 'package:huxiaodui/utils/aliyun_push.dart';
import 'package:huxiaodui/utils/global_controller.dart';
import 'package:huxiaodui/store/store.dart';
import 'package:huxiaodui/components/alarm_dialog.dart';
import 'package:huxiaodui/view/index/map_page/map_controller.dart';

class MobileAppApi {
  /// 单例模式
  static final MobileAppApi _instance = MobileAppApi._internal();
  factory MobileAppApi() {
    return _instance;
  }
  MobileAppApi._internal();

  Future<void> updateDeviceId() async {
    await Api.request(
      url: "/mobileApp/auth",
    );
  }

  Future<MobileAppModel> auth() async {
    SystemPush.unbindAccount();
    Result res = await Api.request(
      url: "/mobileApp/auth",

      failure: (Result res) async {
        GlobalController().logout();
      },
    );
    MobileAppModel mobileApp = MobileAppModel.fromJson(res.data);
    GlobalController().login(mobileApp);
    SystemPush.bindAccount(mobileApp.account);

    return mobileApp;
  }

  Future<void> alarmClear() async {
    await Api.request(
      url: "/mobileApp/alarm/clear",
      data: {},
    );
  }

  Future<void> nowAlarm() async {
    await Api.request(
      url: "/mobileApp/alarm/now",
      data: {},
      success: (Result res) async {
        AlarmDialog.show(res.data);
      },
    );
  }

  Future<void> register({
    required String account,
    required String password,
    required int job,
    required String addr,
  }) async {
    await Api.request(
      isToast: true,
      url: "/mobileApp/register",
      data: {
        "account": account,
        "password": password,
        "job": job,
        "addr": addr,
      },
      success: (Result res) async {
        login(account: account, password: password);
      },
    );
  }

  Future<void> login({
    required String account,
    required String password,
  }) async {
    await Api.request(
      isToast: true,
      url: "/mobileApp/login",
      data: {
        "account": account,
        "password": password,
      },
      success: (Result res) async {
        await Store.auth.save(res.authorization);
        GlobalController.toFirst(showDrawer: true);
        var user = MobileAppModel.fromJson(res.data);
        GlobalController().login(user);
        MapController().updateMarkers();
        Api.mobileApp.getAlarms();
        Api.mobileApp.nowAlarm();
      },
    );
  }

  Future<void> logout() async {
    await Api.request(
      url: "/mobileApp/logout",
      isToast: true,
      success: (res) {
        GlobalController().logout();
      },
    );
  }

  Future<void> delete(String account, String password) async {
    await Api.request(
      isToast: true,
      url: "/mobileApp/delete",
      data: {
        "account": account,
        "password": password,
      },
      success: (Result res) async {
        GlobalController().logout();
      },
    );
  }

  Future<void> update(
      {required String name,
      required String sex,
      required String tel,
      required int job,
      required String addr}) async {
    Api.request(
      url: "/mobileApp/update",
      data: {
        "name": name,
        "sex": sex,
        "tel": tel,
        "job": job,
        "addr": addr,
      },
      isToast: true,
      success: (Result res) {
        GlobalController().user = MobileAppModel.fromJson(res.data);
        GlobalController().refresh();
      },
    );
  }

  Future<void> updatePassword(String password,String newPassword)async {
    await Api.request(
      isToast: true,
      data: {
        "password": password,
        "newPassword": newPassword
      },
      url: "/mobileApp/update/password",
    );
  }

  Future<void> bindEmailCode(String email)async {
    await Api.request(
      isToast: true,
      data: {"email": email},
      url: "/mobileApp/bind/email/code",
    );
  }
  Future<void> bindEmail(String email, String code) async {
    await Api.request(
      isToast: true,
      url: "/mobileApp/bind/email",
      data: {
        "email": email,
        "code": code,
      },
      success: (res) {
        GlobalController().user?.email = email;
        GlobalController().refresh();
      },
    );
  }

  Future<Result> getAlarms() async {
    return Api.request(
      url: "/mobileApp/alarms",
      success: (Result res) async {
        MapController().alarms = AlarmModel.toList(res.data);
        MapController().updateMarkers();
        MapController().refresh();
      },
    );
  }

  Future<void> unbindEmailCode(String email) async {
    await Api.request(
      isToast: true,
      data: {"email": email},
      url: "/mobileApp/unbind/email/code",
    );

  }

}
